home *** CD-ROM | disk | FTP | other *** search
- /********************************/
- /* File: ParseCommands.c */
- /* */
- /* Parse commands coming in from*/
- /* Hypercard. */
- /* */
- /* Once the object is parsed, it*/
- /* is added to the draw list for*/
- /* the window. */
- /* */
- /* Eventually need to match */
- /* multiple tokens so that we */
- /* can have parameters such as: */
- /* BOLD&ITALIC&OUTLINE */
- /* ---------------------------- */
- /* © 1989 Donald Koscheka */
- /* All Rights Reserved */
- /********************************/
-
- #define CR 0x0D
- #define NEWLINE 0x0D
- #define LF 0x0A
- #define TAB 0x09
- #define ETX 0x03 /* the enter key */
-
- long matchToken( buf, tabl )
- Handle buf;
- short tabl;
- /*******************************
- * given an input buffer and the resource
- * id of the parse table, return
- * the token that represents the input
- * string.
- *
- * A token of 0 is returned if no
- * match is found. This way, you
- * can use the first item in the list
- * as the default item!
- *
- * The symbol table should have the
- * format:
- *
- * <string>, <token>
- *
- * where string mathces to the input
- * string and token is that value for
- * a given match.
- *******************************/
- {
- char *bp; /*** pointer to input strings ***/
- Handle strH; /*** handle to parse strings resource ***/
- long token = 0; /*** return the default if no match ***/
- short indx = 0;
- short theID;
- ResType theType;
- short done = 0;
- long len;
- char theNum[31];
- char *np;
- char theName[256];
- char theString[256];
-
- bp = *buf;
-
- while( *bp ){
- toUpper( *bp );
- bp++;
- }
-
- strH = GetResource( 'STR#', tabl );
-
- if( strH ){
-
- GetResInfo( strH, &theID, &theType, &theName );
-
- /*** compare the string to the allowable tokens ***/
- indx = 1 ;
-
- while( !done ){
- theString[0] = '\0';
- GetIndString( &theString, tabl, indx );
-
- if( theString[0] == '\0' ){ /* no strings matched the input */
- done = 1;
- }
- else{ /* attempt to match to current str */
-
- PtoCstr( (char *)&theString );
-
- len = 0;
-
- bp = theString;
- while ( *bp != ',' ){
- bp++;
- len++;
- }
-
- if( strncmp( *buf, (char *)theString, len ) == 0){
- /* have a match so extract the token */
-
- /*** move past any garbage in the string ***/
- while( (*bp < '0' || *bp > '9') && *bp != '-')
- bp++;
-
- /*** now copy what bp points to into a ***/
- /*** a pascal style string ***/
-
- theNum[0] = '\0';
- np = &theNum[1];
-
- while( *bp >= '0' && *bp <= '9' ){
- theNum[0]++;
- *np++ = *bp++;
- }
-
- /*** np is a valid p-string ***/
- StringToNum( theNum, &token );
- done = 1;
- }
- else
- indx++;
- }
- }
- }
-
- return( token );
- }
-
-
- long parseNum( bp )
- char *bp;
- /***********************
- * parse the data stream
- * and return a numeric
- * value. The stream is null
- * terminated.
- *
- ***********************/
- {
- long num = 0;
-
- char theString[256];
- short done = 0;
- char *ps;
-
- /*** move the input pointer until we're looking at a number ***/
- while( *bp && ( *bp < '0' || *bp > '9' ) && *bp != '-' )
- bp++;
-
- /*** copy the data into a pascal string ***/
- ps = theString;
- ps++;
-
- while( *bp >= '0' && *bp <= '9' )
- *ps++ = *bp++;
-
- /*** moved one past the output so try this ***/
- theString[0] = (char)( ps - theString -1 );
- StringToNum( theString, &num );
-
- return( num );
- }
-
- char *nextToken( bp )
- char *bp;
- /***********************
- * given a pointer to an
- * input stream, move to the
- * next token in the stream.
- *
- * Token's are delineated by
- * ',' or whitespace
- *
- * Assumes we are pointing to the current token
- * Move past the current token and the white
- * space that follows it.
- ***********************/
- {
- /*** move past the current token ***/
- while( *bp &&(*bp != ',' && *bp != SPACE && *bp != CR && *bp != LF && *bp != TAB) )
- bp++;
-
- /*** move past the white space to the next token ***/
- while( *bp == ',' || *bp == SPACE || *bp == CR || *bp == LF || *bp == TAB )
- bp++;
-
- return( bp );
- }
-
- void parseRect( buf, theRect )
- Handle buf;
- Rect *theRect;
- /***********************
- * parse the data stream
- * into a rectangle
- *
- * default is the NULL rect
- ***********************/
- {
- char *bp;
-
- theRect->top = theRect->left = theRect->bottom = theRect->right = 0;
-
- if( validHandle( buf ) ){
- HLock( buf );
-
- bp = *buf;
- theRect->top = parseNum( bp );
- bp = nextToken( bp );
-
- theRect->left = parseNum( bp );
- bp = nextToken( bp );
-
- theRect->bottom = parseNum( bp );
- bp = nextToken( bp );
-
- theRect->right = parseNum( bp );
- bp = nextToken( bp );
-
- HUnlock( buf );
- }
- }